package com.cms.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.cms.common.StringUtils;
import com.cms.constant.CmsConstant;
import com.cms.model.PageModel;

public class PageParamWrapperFilter implements Filter{

	private int pageSize;
	
	private int pageIndex;
	
	@Override
	public void destroy() {
		 
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
         req.setCharacterEncoding("utf-8");
         String pageIndexStr = req.getParameter(CmsConstant.PAGE_INDEX);
         String pageSizeStr = req.getParameter(CmsConstant.PAGE_SIZE);
		 PageModel model = new PageModel();
		 if(StringUtils.isNum(pageIndexStr))
		 {
			 model.setPageIndex(StringUtils.str2Int(pageIndexStr));
		 }
		 else
		 {
			 model.setPageIndex(pageIndex);
		 }
		 if(StringUtils.isNum(pageSizeStr))
		 {
			 model.setPageSize(StringUtils.str2Int(pageSizeStr));
		 }
		 else
		 {
			 model.setPageSize(pageSize);
		 }
		 chain.doFilter(req, resp);
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
          pageIndex=StringUtils.str2Int(config.getInitParameter(CmsConstant.PAGE_INDEX));
          pageSize =StringUtils.str2Int(config.getInitParameter(CmsConstant.PAGE_SIZE));
	}

}
